﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPC
{
    /// <summary>
    /// 不良品数量NP控制图
    /// </summary>
    class SpcNP : ISPC
    {

        double[] np;//组内不良数量
        double npbar;//平均不合格品数
        //double []p;//组内不合格率
        double pbar;//平均不合格率
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="samples">每组检查数量</param>
        /// <param name="NGs">每组不良数量</param>
        public SpcNP(int samples, double[] NGs)
        {
            np=NGs;
            npbar = np.Average();
            
            pbar = npbar/samples;
        }
        public double LSL
        { set { } }
        public double USL
        { set { } }
        public double CP()
        {
            return pbar;
        }

        public double CPK()
        {
            return pbar;
        }

        public SCtrlDiagram[] GetCtrlDiagrams()
        {
            double nps = Math.Sqrt(npbar * (1 - pbar));
            double lcl = npbar - 3 * nps;
            SCtrlDiagram[] diags = new SCtrlDiagram[1];
            diags[0] = new SCtrlDiagram()
            {
                diagramName = DiagramName.np,
                points = np,
                cl = npbar,
                lcl = lcl > 0 ? lcl : 0,
                ucl = npbar + 3 * nps
            };
            return diags;
        }

        public double PP()
        {
            return pbar;
        }

        public double PPK()
        {
            return pbar;
        }
    }
}
